package com.hns.can.service.processor;

import com.hns.can.service.service.BaseDriverExamInfoService;
import com.hns.frame.processor.TaskProcessor;
import com.hns.tool.pub.PubUtil;
import com.hns.tool.pub.SearchConditionVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.task.TaskExecutor;

import java.util.HashMap;
import java.util.Map;

public class UptEnerConsProcessor implements TaskProcessor {
    
    // 执行器
    private TaskExecutor              taskExecutor;
    private final Logger              logger = LoggerFactory.getLogger(UptEnerConsProcessor.class);
    
    private BaseDriverExamInfoService baseDriverExamInfoService;
    private SearchConditionVo conditionVo;
    
    public UptEnerConsProcessor(TaskExecutor taskExecutor, BaseDriverExamInfoService baseDriverExamInfoService, SearchConditionVo conditionVo) {
        this.taskExecutor = taskExecutor;
        this.baseDriverExamInfoService = baseDriverExamInfoService;
        this.conditionVo = conditionVo;
    }
    
    public void process() throws Exception {
        // 使用多线程处理
        taskExecutor.execute(new Runnable() {
            public void run() {
//                 调用切面技术更新“T_COMM_ENER_CONS_STAT_INFO”对应的“DRV_ID”
                try {
                    baseDriverExamInfoService.uptEnerConsStatInfoDrvId(conditionVo);
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.debug("update energy consumption table's drvId failure");
                }
                
                Map<String, Object> paraMap = new HashMap<String, Object>();
                paraMap.put("corpId", conditionVo.getCorpId());
                paraMap.put("beginTime", conditionVo.getBeginTime().substring(0, 10));
                paraMap.put("endTime", conditionVo.getEndTime().substring(0, 10));
                // 调用存储过程“PC_BS_TMP_ENE_DET”重新生成“T_BS_TMP_ENE_DET”数据
                try {
//                    baseDriverExamInfoService.buildEnerConsDetailMidTab(paraMap);
                    //modifiy by qianjy 增加调取存储过程反馈信息的接受处理
                    if(PubUtil.isNotEmpty(paraMap.get("oRetCode")) && "-1".equals(paraMap.get("oRetCode").toString())){
                    	throw new Exception(paraMap.get("oRetNote").toString());
                    };
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.debug("Run the stored procedure to create T_BS_TMP_ENE_DET failure");
                }
                // 调用存储过程“PC_BS_DAY_ENE_DRV”重新生成“T_BS_DAY_ENE_DRV”数据
                try {
//                    baseDriverExamInfoService.buildEnerConsStatDrvMidTab(paraMap);
                    //modifiy by qianjy 增加调取存储过程反馈信息的接受处理
                    if(PubUtil.isNotEmpty(paraMap.get("oRetCode")) && "-1".equals(paraMap.get("oRetCode").toString())){
                    	throw new Exception(paraMap.get("oRetNote").toString());
                    };
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.debug("Run the stored procedure to create T_BS_DAY_ENE_DRV failure");
                }
            }
        });
    }
    
    public void setTaskExecutor(TaskExecutor taskExecutor) {
        this.taskExecutor = taskExecutor;
    }
    
    public void setBaseDriverExamInfoService(BaseDriverExamInfoService baseDriverExamInfoService) {
        this.baseDriverExamInfoService = baseDriverExamInfoService;
    }
    
    public void setConditionVo(SearchConditionVo conditionVo) {
        this.conditionVo = conditionVo;
    }
    
}
